import React, { Component } from 'react'
import {
  BrowserRouter as Router,
  Redirect,
  Route,
  Switch
} from 'react-router-dom'

import Login from './Login'
import Layout from './Layout'
import NotFound from '../router-base-v5/NotFound'
import Order from '../router-v5-nest/Order'
export default class Index extends Component {
  render () {
    return (
      <Router>
        <div>
          <Switch>
            <Route path='/login' component={Login} />
            <Route
              path='/layout'
              render={props => {
                const token = localStorage.getItem('token')
                if (token) {
                  // 登录了
                  return <Layout {...props} />
                } else {
                  return (
                    <Redirect
                      to={`/login?redirect=${encodeURI(
                        props.location.pathname + props.location.search
                      )}`}
                    />
                  )
                }
              }}
            ></Route>

            <Route
              path='/order'
              render={props => {
                const token = localStorage.getItem('token')
                if (token) {
                  // 登录了
                  return <Order {...props} />
                } else {
                  return (
                    <Redirect
                      to={`/login?redirect=${encodeURI(
                        props.location.pathname + props.location.search
                      )}`}
                    />
                  )
                }
              }}
            ></Route>

            {/* 重定向 */}
            <Redirect exact from='/' to='/login' />

            {/* 404 */}
            <Route path='*' component={NotFound} />
          </Switch>
        </div>
      </Router>
    )
  }
}
